#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int arr[8] = { 1,2,3,4,5,6,7,8 };
int main()
{
	do {
		for (int i = 0; i < 8; i++) {
			cout << arr[i];
			if (i != 7) {
				cout << " ";
			}
		}
		cout << endl;
	} while (next_permutation(arr,arr+8));
	return 0;
}